﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Fc
{
    public partial class JinDu3 : System.Web.UI.Page
    {
        private string sql = @"select ID,日期,锁定,磷化支数,填报人
,case when 产品类别ID=1 then '油管接箍(J55)'
when 产品类别ID=2 then '套管接箍(J55)'
when 产品类别ID=3 then '油管接箍(N80Q)'
when 产品类别ID=4 then '套管接箍(N80Q)'
else '未知接箍' end as 产品类别
,case when 工段=1 then '甲班'
when 工段=2 then '乙班'
when 工段=3 then '丙班'
when 工段=4 then '丁班'
else '未知班组' end as 班组
from [生产进度3] where 1=1";

        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            if (this.ChanPin.Text != "0")
            {
                sql += " and [产品类别ID]=" + this.ChanPin.Text.Replace("'", "");
            }

            if (this.GongDuan.Text != "0")
            {
                sql += " and [工段]=" + this.GongDuan.Text.Replace("'", "");
            }

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and [日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and [日期]<='" + Dt.ToShortDateString() + "'";
            }
            sql += " order by [日期] desc,[工段] asc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.GridView1.SelectedIndex = -1;
            this.Button_Delete.Enabled = false;
            this.Button_Edit.Enabled = false;
            this.Button_Lock.Enabled = false;
            this.Button_ZhiJian.Enabled = false;
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "delete from [生产进度3] where 锁定=0 and [id]= " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
            this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(bool)GridView1.DataKeys[GridView1.SelectedIndex].Values[1])
            {
                this.Button_Lock.Enabled = true;
                this.Button_Delete.Enabled = true;
            }
            else
            {
                this.Button_Lock.Enabled = false;
                this.Button_Delete.Enabled = false;
            }
            this.Button_Edit.Enabled = true;
            this.Button_Edit.OnClientClick = "parent.swin2_load('/Fc/JinDu3_In.aspx?id="
                + (int)GridView1.DataKeys[GridView1.SelectedIndex].Values[0] + "',640,480);";

            this.Button_ZhiJian.Enabled = true;
            this.Button_ZhiJian.OnClientClick = "parent.swin2_load('/Fc/接箍质检数据录入.aspx?bid="
                + (int)GridView1.DataKeys[GridView1.SelectedIndex].Values[0] + "',640,558);";
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Lock_Click(object sender, EventArgs e)
        {
            string id = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
            this.SqlDataSource1.UpdateCommand = "declare @d date \nselect @d=日期 from 生产进度3 where [id]="
                + id + "\nupdate 生产进度3 set 锁定=1 where 日期=@d";
            this.SqlDataSource1.Update();
        }
        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongDuan.Text = "0";
            this.ChanPin.Text = "0";
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

    }
}
